package com.sl.hx.shop.ui.home.presenter;

import android.content.Context;

import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.HotSearch;
import com.sl.hx.shop.bean.ProductList;
import com.sl.hx.shop.bean.SearchResult;
import com.sl.hx.shop.bean.ShopDetail;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.contacts.GoodsListContacts;
import com.sl.hx.shop.ui.home.contacts.SearchContacts;
import com.sl.hx.shop.utils.SPUtils;
import com.sl.hx.shop.utils.SearchHistoryUtil;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.List;

import io.reactivex.Observable;

public class SearchPresenter extends BasePresenter<SearchContacts.view> implements SearchContacts.presenter {

    private Context mContext;


    public SearchPresenter(Context context) {
        this.mContext=context;

    }

    /**
     * 从本地获取保存的历史搜索记录
     */
    public void getAllSearchHistory() {
        getView().getAllSearchHistorySuccess(SearchHistoryUtil.getHistory(10));
    }

    /**
     * 清空历史搜索记录
     */
    public void deleteAllHistory() {
        SPUtils.getInstance().remove(SPUKey.SEARCHHISTORY);

    }

    public void getHotSearch() {
        Observable observable = ApiFactory.getInstance().getHotSearch(ApiRetrofit.getCacheControl());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<HotSearch>>(mContext) {
            @Override
            protected void _onNext(List<HotSearch> hotSearches) {
                getView().ShowHotSearch(hotSearches);
            }

            @Override
            protected void _onError(String message) {
            }

        }, true, getView(), ActivityEvent.DESTROY);
    }
}
